<template>
  <div class="set-role-container">
    <el-form label-position="left" label-width="80px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="setRoleSuperUserInfo.username" disabled />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="setRoleSuperUserInfo.nickname" disabled />
      </el-form-item>
      <el-form-item label="角色名称">
        <el-select v-model="roleId" placeholder="请选择角色名称">
          <el-option v-for="role in roles" :key="role.id" :label="role.nickname" :value="role.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('close-set-role')">取 消</el-button>
      <el-button type="primary" @click="setRole">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { getRoles } from '@/api/role'
import { setRole } from '@/api/super_user'
export default {
  name: 'SetRole',
  components: {},
  props: {
    setRoleSuperUserInfo: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      roles: [],
      roleId: this.setRoleSuperUserInfo.role_id || null
    }
  },
  computed: {},
  watch: {},
  created () {
    this.getRoles()
  },
  mounted () {},
  methods: {
    getRoles: async function () {
      const data = {
        pageIndex: 1,
        pageRow: 10000,
        rolename: ''
      }
      const res = await getRoles(data)
      if (res.list[0]) {
        this.roles = res.list
      }
    },
    setRole: async function () {
      const data = {
        id: this.setRoleSuperUserInfo.id,
        roleId: this.roleId
      }
      const res = await setRole(data)
      if (!res) return this.$message({ message: '没有改动!', type: 'error', duration: 1000 })
      this.$message({ message: '角色分配成功', type: 'success', duration: 1000 })
      this.$emit('reset-super-users')
    }
  }
}
</script>

<style scoped lang="scss"></style>
